package net.simflow.swf.analyzer.chart;

import java.util.Collection;
import java.util.Map.Entry;
import java.util.TreeMap;

import net.simflow.swf.analyzer.gui.chart.ChartCreator;
import net.simflow.swf.analyzer.gui.chart.ChartGUI;
import net.simflow.swf.analyzer.network.data.ElementInNetwork;

import org.eclipse.swt.widgets.Display;

public class StatsChart {

	private static StatsChart instance = new StatsChart();

	private StatsChart() {
	}

	public static StatsChart getInstance() {
		return instance;
	}

	public void drawDistributionChart(
			Display display,
			String chartTitle,
			String xAxisTitle,
			String yAxisTitle,
			TreeMap<? extends Number, ? extends Collection<? extends Object>> distribution) {
		if (distribution == null) {
			return;
		}
		distribution.comparator();

		double[] x = new double[distribution.size()];
		double[] y = new double[distribution.size()];
		String[] tip = new String[distribution.size()];

		int i = 0;
		for (Entry<? extends Number, ? extends Collection<? extends Object>> en : distribution
				.entrySet()) {
			Collection<? extends Object> cll = en.getValue();
			StringBuffer strb = new StringBuffer("Item: ");
			for (Object ele : cll) {
				if (strb.length() > 70) {
					strb.append("...");
					break;
				}
				try {
					strb.append(((ElementInNetwork) ele).getNameInNetwork())
							.append(";");
				} catch (Exception ee) {
					strb.append(ele.toString()).append(";");
				}
			}
			strb.append("\nCount: " + cll.size());

			x[i] = en.getKey().doubleValue();
			y[i] = cll.size();
			tip[i] = strb.toString();
			i++;
		}

		ChartGUI gui = new ChartGUI(display, chartTitle);
		ChartCreator.getInstance().createLineChart(gui.getComposite(),
				chartTitle, xAxisTitle, yAxisTitle, x, y, tip, chartTitle);
		gui.open();
	}
}
